<?php
   require_once( 'contest.class.php' ); 
   require_once( 'contest_team.class.php' );
   require_once( 'team_problem.class.php' ); 
   require_once( 'contest_problem_list.class.php' ); 
       
   class contest_problem_submit{
               private $contest_no;
               private $contest;  
               private $db; 
               private $timenow;
               private $team_no;
               private $problem_no;  
               public function contest_problem_submit($db,$contestname,$team_no,$problem_no,$codetxt){
                   date_default_timezone_set('PRC');  
                   $this->timenow=date("YmdHis");
                   $this->db=$db;
                   $this->team_no=$team_no;
                   $this->problem_no=$problem_no;
                   $this->contest=new contest($db);
                   $this->contest->load('name',$contestname); 
                   $this->contest_no=$this->contest->init_contest_no();                          
                   if($this->contest_no==-1){
                              throw new Exception("竞赛名不存在",7); 
                              return;
                   }
                    if(!$this->is_team_in_contest($team_no)){
                              throw new Exception("队伍不在本次竞赛",8); 
                              return ;
                   }                     
                   if(!$this->is_problem_in_contest($problem_no)){
                              throw new Exception("题号不在竞赛",8); 
                              return ;
                   }
                   $this->solve($problem_no,$team_no,$codetxt);
                  
                              
               }           
              /**
               * 试题是否在本次竞赛               * 
               */
               private function is_problem_in_contest($problem_no){ 
                   $newproblem=new contest_problem_list($this->db);
                   if($newproblem->load($this->contest_no,$problem_no)==false)                        
                      return false;
                   else 
                      return true; 
               }
               /**
               * 通过一个数组返回竞赛全部信息
               * 
               */
               public function is_team_in_contest($team_no){
                    $iscontestteam=new contest_team($this->db); 
                    if($iscontestteam->load($this->contest_no,$team_no)==false)//没有
                          return false;  
                    else 
                         return true; 
                    
               }   
               
               /**
               * 检测代码并写数据库
               *             
               * @param mixed 题号
               * @param mixed 队伍号
               * @param mixed代码
               */
               private function solve($problem_no,$team_no,$txt){

                  $teamproblem=new team_problem($this->db);
                  $isnotfirst= $teamproblem->load($team_no,$problem_no);
                   //载入成功，则不是第一次添加，如果不是
                   //这里重载了db_obj.class.php函数load因为主码为2个字段
                   $stat=null;
                   $subtimes=0;
                   if($isnotfirst==true){
                       $stat=$teamproblem->get('stat');
                       $subtimes=$teamproblem->get('subtimes');  
                   }   
                   /****需要一个函数测试代码********/                 
                       $result=jiaderesult($problem_no,$team_no,$txt);//$result =后台测试($problem_no,$team,$txt)                                         
                      
                   if($result==='right') {
                        if($stat==='right')
                             return true;  //原来正确的不修改数据库
                        if($isnotfirst==false){ 
                            $teamproblem->set('team_no',$team_no);
                            $teamproblem->set('problem_no',$problem_no);
                            $teamproblem->set('lasttime',$this->timenow);
                            $teamproblem->set('subtimes',0);
                            $teamproblem->set('stat',$result);
                            $teamproblem->save();  
                     
                        }
                        else{
                            
                            $teamproblem->set('lasttime',$this->timenow);                         
                            $teamproblem->set('stat',$result);
                            $teamproblem->update();                            
                            //这里重载了db_obj.class.php函数update因为主码为2个字段  
                        }
                        return true;
                      
                   }
                   else{
                        if($stat==='right')
                             return false;  //原来正确的不修改数据库
                             $subtimes+=1;
                        if($isnotfirst==false){                              
                              $teamproblem->set('team_no',$team_no);
                              $teamproblem->set('problem_no',$problem_no);
                              $teamproblem->set('lasttime','00000000000000');
                              $teamproblem->set('subtimes',$subtimes);
                              $teamproblem->set('stat',$result);   
                              $teamproblem->save();  
                              
                        }
                        else{  
                            $teamproblem->set('subtimes',$subtimes);                         
                            $teamproblem->set('stat',$result);
                            $teamproblem->update(); 
                         
                        }
                        return false;
                   }
                   
               }
               public function getresult(){
                    $result=array();
                    $teamproblem=new team_problem($this->db);
                    $teamproblem->load($this->team_no,$this->problem_no);
                    $result['team_no']=$teamproblem->get('team_no');
                    $result['problem_no']=$teamproblem->get('problem_no');
                    $result['lasttime']=$teamproblem->get('lasttime');
                    $result['subtimes']=$teamproblem->get('subtimes');
                    $result['stat']=$teamproblem->get('stat'); 
                    return $result;
                   
               }
             
              
   }
?>
